package crap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

import tools.Arrow;

public class Erklaerer02 {
	public static void main(String[] args) {
		final double[] histo = new double[8];
		
		for (int i = 0; i < histo.length; i++) {
			histo[i] = Math.random()*300;
		}
		
		JFrame ff = new JFrame();
		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Point2D a = new Point2D.Double(100,100), b = new Point2D.Double(500,300);
		
		double arrowWidth = 10, arrowHeight = 40;
		
		final Arrow arrw = new Arrow();
		arrw.setArrow(a, b, arrowWidth, arrowHeight);
		final AffineTransform at = new AffineTransform();
		
		final AffineTransform rotat0r = new AffineTransform();
		final Point2D centre = new Point2D.Double(400,300);
		final JComponent canvas = new JComponent() {
			{setPreferredSize(new Dimension(800,600));}
			
			@Override
			public void paint(Graphics g) {
				Graphics2D g2 = (Graphics2D)g;

				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
			    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
			    g2.transform(at);
			    Point2D point = new Point2D.Double();
			    for (int i = 0; i < histo.length; i++) {
					point.setLocation(1,0);
					rotat0r.setToRotation((i*2*Math.PI)/8.-Math.PI);
					rotat0r.transform(point, point);
					point.setLocation(histo[i]*point.getX()+centre.getX(), histo[i]*point.getY()+centre.getY());
					arrw.setArrow(centre, point, 3, 10);
				    g2.setColor(Color.red);
					g2.fill(arrw);
					g2.setColor(Color.black);
					g2.draw(arrw);
				}
			    
				super.paint(g);
			}
		};
		canvas.addMouseWheelListener(new MouseWheelListener() {
			
			@Override
			public void mouseWheelMoved(MouseWheelEvent e) {
				
				double scale = Math.pow(1.01, e.getWheelRotation());

				try {
					Point2D p = new Point2D.Double(e.getX(), e.getY()); 
					at.inverseTransform(p, p);
					at.concatenate(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
					at.concatenate(AffineTransform.getScaleInstance(scale, scale));
					at.concatenate(AffineTransform.getTranslateInstance(-p.getX(), -p.getY()));
				}
				catch (NoninvertibleTransformException nte ) {
					nte.printStackTrace();
				}
				canvas.repaint();
			}
		});
		ff.add(canvas);
		ff.pack();
		ff.setVisible(true);
	}
}
